#include <jni.h>
#include <string>
#include <android/log.h>
#define LOGE(...) __android_log_print(ANDROID_LOG_ERROR,__VA_ARGS__ ,__VA_ARGS__) // 定义LOGE类型
class Student{
public:
    void print_msg(char * msg){
        LOGE("打印消息",msg);
    }
};

/**
 * 自定义的空指针异常
 */
class NullPointException{
private:
    char * msg;
public:
    NullPointException(char * msg){
        this->msg = msg;
    }
    char * get_msg(){
        return this->msg;
    }
};

extern "C"
JNIEXPORT void JNICALL
Java_com_itfitness_exceptiondemo_MainActivity_callNativeException(JNIEnv *env, jobject instance) {
    Student* student = new Student();
    student->print_msg("我是消息");
    //将Student指针赋值为NULL，制造一个空指针异常
    student = NULL;
    try {
        if(student == NULL){
            //当student是NULL的时候就抛一个自定义的空指针异常
            throw NullPointException("Native Student 空指针异常");
        } else{
            student->print_msg("我还是消息");
        }
    }catch (NullPointException e){
        LOGE("捕捉异常：",e.get_msg());
        //捕捉异常后将异常再抛给Java层
        jclass j_exception = env->FindClass("java/lang/NullPointerException");
        env->ThrowNew(j_exception,e.get_msg());
    }
}